package com.whiteape.book.presenter;


import android.widget.Toast;

import androidx.annotation.NonNull;

import com.hwangjr.rxbus.RxBus;
import com.whiteape.book.contract.RecommendContract;
import com.whiteape.book.model.type.BookListType;
import com.whiteape.book.model.type.BookSortListType;
import com.whiteape.book.model.zssq.BookListBean;
import com.whiteape.book.model.zssq.BookListDetailBean;
import com.whiteape.book.model.zssq.SortBookBean;
import com.whiteape.book.model.zssq.remote.RemoteRepository;
import com.whiteape.book.ui.base.RxPresenter;
import com.whiteape.book.utils.RxUtils;
import com.whiteape.book.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by newbiechen on 17-5-1.
 */

public class RecommendPresenter extends RxPresenter<RecommendContract.View> implements RecommendContract.Presenter {

    private Disposable disposable;

    @Override
    public void initData(String tag) {
        if (disposable != null) {
            return;
        }
        getBookListSingle(BookListType.HOT, tag, 0, 20)
                .compose(RxUtils::toSimpleSingle)
                .subscribe(new SingleObserver<List<BookListBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onSuccess(List<BookListBean> recyclerViewData) {
//                        mView.upData(recyclerViewData);

                        for(BookListBean bean : recyclerViewData) {
                            getBookListDetail(bean.get_id());
                        }
                        disposable.dispose();
                        disposable = null;

                    }

                    @Override
                    public void onError(Throwable e) {
//                        Toast.makeText(mView.getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        mView.showError();
                        ToastUtils.show(e.getMessage());
                        disposable.dispose();
                        disposable = null;
                    }
                });

    }

    private void getBookListDetail(String detailId) {
        List<Disposable> disposables = new ArrayList<>(1);
        RemoteRepository.getInstance().getBookListDetail(detailId)
                .compose(RxUtils::toSimpleSingle)
                .subscribe(new SingleObserver<BookListDetailBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposables.add(d);

                    }

                    @Override
                    public void onSuccess(BookListDetailBean bookListDetailBean) {

                        if (mView == null) {
                            return;
                        }
                        mView.upData(bookListDetailBean);
                        for (Disposable disposable : disposables) {
                            disposable.dispose();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                        for (Disposable disposable : disposables) {
                            disposable.dispose();
                        }

                    }
                });
    }
//
//    public void refreshBookList(BookListType type, String tag, int start, int limited) {
//
//        if (tag.equals("全本")){
//            tag = "";
//        }
//
//        Disposable refreshDispo = getBookListSingle(type, tag, start, limited)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        (beans)-> {
//                            mView.finishRefresh(beans);
//                            mView.complete();
//                        }
//                        ,
//                        (e) ->{
//                            mView.complete();
//                            mView.showError();
////                            LogUtils.e(e);
//                        }
//                );
////        addDisposable(refreshDispo);
//    }
//
//    public void loadBookList(BookListType type, String tag, int start, int limited) {
//        Disposable refreshDispo = getBookListSingle(type, tag, start, limited)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        (beans)-> {
//                            mView.finishLoading(beans);
//                        }
//                        ,
//                        (e) ->{
//                            mView.showLoadError();
////                            LogUtils.e(e);
//                        }
//                );
////        addDisposable(refreshDispo);
//    }
//
    private Single<List<BookListBean>> getBookListSingle(BookListType type, String tag, int start, int limited){
        Single<List<BookListBean>> bookListSingle = null;
        //数据类型转换
        String gender = "";
        if (tag.equals("男生")){
            gender = "male";
            tag = "";
        }
        else if (tag.equals("女生")){
            gender = "female";
            tag = "";
        }

        switch (type){
            case HOT:
                bookListSingle = RemoteRepository.getInstance()
                        .getBookLists(type.getNetName(),"collectorCount",start,limited,tag,gender);
                break;
            case NEWEST:
                bookListSingle = RemoteRepository.getInstance()
                        .getBookLists("all",type.getNetName(),start,limited,tag,gender);
                break;
            case COLLECT:
                bookListSingle = RemoteRepository.getInstance()
                        .getBookLists("all",type.getNetName(),start,limited,tag,gender);
                break;
        }
        return bookListSingle;
    }


    @Override
    public void refreshSortBook(String gender, BookSortListType type, String major, String minor, int start, int limit) {

        if (minor.equals("全部")){
            minor = "";
        }

        Disposable refreshDispo = RemoteRepository.getInstance()
                .getSortBooks(gender,type.getNetName(),major,minor,start,limit)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (beans)-> {
                            mView.finishRefresh(beans);
                            mView.complete();
                        }
                        ,
                        (e) ->{
                            mView.showError();
                            mView.complete();
//                            mView.showError();
//                            LogUtils.e(e);
                        }
                );
        addDisposable(refreshDispo);
    }

}
